package com.yanqu.road.server.logic.thread.translate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ChannelInfo;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.sdk.TranslateLanguageMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/9/14 17:13
 */
public class MicrosoftTranslateLanguageTask extends GeneralTask {

    GamePlayer player;
    String content;
    String language;
    long msgIndex;

    public MicrosoftTranslateLanguageTask(GamePlayer player, String content, String language, long msgIndex) {
        this.player = player;
        this.content = content;
        this.language = TranslateLanguageMgr.languageCodeTransfer(language);
        this.msgIndex = msgIndex;
    }

    private static final Logger log = LogManager.getLogger(MicrosoftTranslateLanguageTask.class);
    @Override
    public void doRun() {
        try {
            if (ServerListMgr.isOpenTranslateChat()) {
                ChannelInfo channelConfig = ServerListMgr.getChannelInfoMap().get(ServerListMgr.getServerInfo(ConfigHelper.getLong("serverId")).getChannelId());
                String[] translationsParamArr = channelConfig.getTranslateUrl().split("\\|");
                String microsoftLanguage = TranslateLanguageMgr.languageCodeGoogleTransferMicrosoft(language);
                String url = translationsParamArr[0] + "&to=" + microsoftLanguage;
                String key = translationsParamArr[1];
                String afterContent = TranslateLanguageMgr.getContent(this.content, language);
                if (afterContent == null) {
                    JSONArray postData = new JSONArray();
                    JSONObject contentData = new JSONObject();
                    contentData.put("Text", this.content);
                    postData.add(contentData);
                    Map<String, String> headerMap = new HashMap<>();
                    headerMap.put("Ocp-Apim-Subscription-Key", key);
                    String result = HttpHelper.doPostJsonTimeOut(url, postData.toJSONString(), "UTF-8", headerMap);
                    JSONArray resultJsonArray = JSONArray.parseArray(result);
                    JSONObject resultJsonObject = (JSONObject)resultJsonArray.get(0);
                    afterContent = ((JSONObject)resultJsonObject.getJSONArray("translations").get(0)).getString("text");

                    // 本地敏感词过滤下
                    if (ForbiddenWordMgr.isForbidden(afterContent)) {
                        afterContent = "***";
                    }

                    TranslateLanguageMgr.addTranslateContent(content, language, afterContent);
                }
                WorldMessageProto.TranslationRespMsg.Builder msg = WorldMessageProto.TranslationRespMsg.newBuilder();
                msg.setRet(0);
                msg.setAfterTranslateContent(afterContent);
                msg.setMsgIndex(msgIndex);
                player.sendPacket(ClientProtocol.U_TRANSLATE_LANGUAGE, msg);
            }
        } catch (Exception ex) {
            log.error(ex);
        }
    }
}
